package cn.edu.sgu.www.mhxysy.service.chongwu_equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu_equipment.ChongwuTaozhuangDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuTaozhuangCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuTaozhuangCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.chongwu_equipment.ChongwuTaozhuangPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu_equipment.ChongwuTaozhuangCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuTaozhuangCategoryServiceImpl implements ChongwuTaozhuangCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final ChongwuTaozhuangCategoryMapper chongwuTaozhuangCategoryMapper;

	@Autowired
	public ChongwuTaozhuangCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			ChongwuTaozhuangCategoryMapper chongwuTaozhuangCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.chongwuTaozhuangCategoryMapper = chongwuTaozhuangCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_CWTZ;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(ChongwuTaozhuangDTO chongwuTaozhuangDTO) {
		ChongwuTaozhuangCategory chongwuTaozhuangCategory = toEntity(chongwuTaozhuangDTO);

		chongwuTaozhuangCategory.setId(null);

		chongwuTaozhuangCategoryMapper.insert(chongwuTaozhuangCategory);
	}

	@Override
	public void updateById(ChongwuTaozhuangDTO chongwuTaozhuangDTO) {
		ObjectUtils.requireNonNull(chongwuTaozhuangDTO.getId(), CustomMessages.ID_NOT_NULL);

		ChongwuTaozhuangCategory chongwuTaozhuangCategory = toEntity(chongwuTaozhuangDTO);

		chongwuTaozhuangCategoryMapper.updateById(chongwuTaozhuangCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<ChongwuTaozhuangCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				chongwuTaozhuangCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<ChongwuTaozhuangCategory> selectAll() {
		return chongwuTaozhuangCategoryMapper.selectList(null);
	}

	@Override
	public ChongwuTaozhuangCategory selectById(Integer id) {
		return chongwuTaozhuangCategoryMapper.selectById(id);
	}

	@Override
	public Page<ChongwuTaozhuangCategory> selectByPage(ChongwuTaozhuangPager pager) {
		QueryWrapper<ChongwuTaozhuangCategory> queryWrapper = new QueryWrapper<>();
		Page<ChongwuTaozhuangCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getChongwuId() != null,
				"chongwu_id", pager.getChongwuId()
		);
		queryWrapper.like(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);

		return chongwuTaozhuangCategoryMapper.selectPage(page, queryWrapper);
	}

	private ChongwuTaozhuangCategory toEntity(ChongwuTaozhuangDTO chongwuTaozhuangDTO) {
		Integer id = chongwuTaozhuangDTO.getId();
		String name = chongwuTaozhuangDTO.getName();

		// 验证外观名称是否重复
		QueryWrapper<ChongwuTaozhuangCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(id != null, "id", id);
		queryWrapper.eq("name", name);

		Long count = chongwuTaozhuangCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "套装名称重复！");
		}

		ChongwuTaozhuangCategory chongwuTaozhuangCategory = new ChongwuTaozhuangCategory();

		chongwuTaozhuangCategory.setId(chongwuTaozhuangDTO.getId());
		chongwuTaozhuangCategory.setName(chongwuTaozhuangDTO.getName());
		chongwuTaozhuangCategory.setSkillId(chongwuTaozhuangDTO.getSkillId());
		chongwuTaozhuangCategory.setWaiguanId(chongwuTaozhuangDTO.getWaiguanId());
		chongwuTaozhuangCategory.setChongwuId(chongwuTaozhuangDTO.getChongwuId());

		return chongwuTaozhuangCategory;
	}

}